package endcourseproject;

public class MCPS_dataIndication extends ZTCMsg {

	public static final byte AddrModeNone  = (byte)0;
	public static final byte AddrModeShort  = (byte)2;
	public static final byte AddrModeLong  = (byte)3;
	public static final byte[] DefaultPanId = {(byte)0xBE,(byte)0xBA};  
	public static final byte[] BroadcastAddr = {(byte)0xFF,(byte)0xFF};  

	public static final byte McpsNwkOpcodeGroup = (byte)0x86;    // MCPS_NWK_SapHandler          

	// MCPS Opcode ID's
	public static final byte McpsDataInd = (byte)0x01;  // data indication

	public static final int dstAddrOffset = 0;   
	public static final int dstAddrLen = 8;   
	//private byte[] dstAddr;    // First 0/2/8 bytes is the address as defined by dstAddrMode
	public static final int dstPanIdOffset = dstAddrLen;   
	public static final int dstPanIdLen = 2;   
	//private byte[] dstPanId;   // 16 bit word converted to little endian byte array
	public static final int dstAddrModeOffset = dstPanIdOffset + dstPanIdLen;   
	public static final int dstAddrModeLen = 1;   
	//private byte dstAddrMode;	  
	public static final int srcAddrOffset = dstAddrModeOffset + dstAddrModeLen;   
	public static final int srcAddrLen = 8;   
	//private byte[] srcAddr;    // First 0/2/8 bytes is the address as defined by srcAddrMode	  
	public static final int srcPanIdOffset = srcAddrOffset + srcAddrLen;   
	public static final int srcPanIdLen = 2;   
	//private byte[] srcPanId;   // 16 bit word converted to little endian byte array
	public static final int srcAddrModeOffset = srcPanIdOffset + srcPanIdLen;   
	public static final int srcAddrModeLen = 1;   
	//private byte srcAddrMode;
	public static final int msduLengthOffset = srcAddrModeOffset + srcAddrModeLen;   
	public static final int msduLengthLen = 1;   
	//private byte msduLength;    // 0-102 (101?)
	public static final int mpduLinkQualityOffset = msduLengthOffset + msduLengthLen;   
	public static final int mpduLinkQualityLen = 1;   
	//private byte mpduLinkQuality;
	public static final int securityUseOffset = mpduLinkQualityOffset + mpduLinkQualityLen;   
	public static final int securityUseLen = 1;   
	//private byte securityUse; 
	public static final int aclEntryOffset = securityUseOffset + securityUseLen;   
	public static final int aclEntryLen = 1;   
	//private byte aclEntry;
	public static final int msduOffset = aclEntryOffset + aclEntryLen;   
	public static final int msduLen = 1;   

	public static final int MIN_SIZE = msduOffset;   

	//private byte[] msdu;        //

	public MCPS_dataIndication(ZTCMsg msg)
	{
		super(McpsNwkOpcodeGroup, McpsDataInd, msg.getLen(), msg.getData());
	}
	
	
	public byte[] getDstAddr() {
		byte[] dstAddr = new byte[dstAddrLen];
		System.arraycopy( this.getData(), dstAddrOffset,dstAddr, 0, dstAddr.length);
		return dstAddr;
	}

	public void setDstAddr(byte[] dstAddr) {
        if (dstAddr == null)
        {
        	setDstAddrMode(AddrModeNone);
        	return;
        }		
        else if (dstAddr.length == dstAddrLen)
        {
        	setDstAddrMode(AddrModeLong);
        }
        else if (dstAddr.length == 2)
        {
        	setDstAddrMode(AddrModeShort);
        }
		System.arraycopy( dstAddr, 0,this.getData(), dstAddrOffset, dstAddr.length);
	}

	public byte[] getDstPanId() {
		byte[] dstPanId = new byte[dstPanIdLen];
		System.arraycopy( this.getData(), dstPanIdOffset,dstPanId, 0, dstPanId.length);
		return dstPanId;
	}
	public void setDstPanId(byte[] dstPanId) {
		if (dstPanId != null){			
			System.arraycopy( dstPanId, 0,this.getData(), dstPanIdOffset, dstPanId.length);
		}
	}
	public byte getDstAddrMode() {
		return this.getData()[dstAddrModeOffset];
	}
	public void setDstAddrMode(byte dstAddrMode) {
		this.getData()[dstAddrModeOffset] = dstAddrMode;
	}
	public byte[] getSrcAddr() {
		byte[] srcAddr = new byte[srcAddrLen];
		System.arraycopy( this.getData(), srcAddrOffset,srcAddr, 0, srcAddr.length);
		return srcAddr;
	}
	public void setSrcAddr(byte[] srcAddr) {
        if (srcAddr == null)
        {
        	setSrcAddrMode(AddrModeNone);
        	return;
        }		
        else if (srcAddr.length == dstAddrLen)
        {
        	setSrcAddrMode(AddrModeLong);
        }
        else if (srcAddr.length == 2)
        {
        	setSrcAddrMode(AddrModeShort);
        }
		System.arraycopy( srcAddr, 0,this.getData(), srcAddrOffset, srcAddr.length);
	}
	public byte[] getSrcPanId() {
		byte[] srcPanId = new byte[srcPanIdLen];
		System.arraycopy( this.getData(), srcPanIdOffset,srcPanId, 0, srcPanId.length);
		return srcPanId;
	}
	public void setSrcPanId(byte[] srcPanId) {
		if (srcPanId != null){			
			System.arraycopy( srcPanId, 0,this.getData(), srcPanIdOffset, srcPanId.length);
		}
	}
	public byte getSrcAddrMode() {
		return this.getData()[srcAddrModeOffset];
	}
	public void setSrcAddrMode(byte srcAddrMode) {
		this.getData()[srcAddrModeOffset] = srcAddrMode;
	}
	public byte getMsduLength() {
		return this.getData()[msduLengthOffset];
	}
	public void setMsduLength(byte msduLength) {
		this.getData()[msduLengthOffset] = msduLength;
	}
	public byte getMpduLinkQuality() {
		return this.getData()[mpduLinkQualityOffset];
	}
	public void setMpduLinkQuality(byte mpduLinkQuality) {
		this.getData()[mpduLinkQualityOffset] = mpduLinkQuality;
	}
	public byte getSecurityUse() {
		return this.getData()[securityUseOffset];
	}
	public void setSecurityUse(byte securityUse) {
		this.getData()[securityUseOffset] = securityUse;
	}
	public byte getAclEntry() {
		return this.getData()[aclEntryOffset];
	}
	public void setAclEntry(byte aclEntry) {
		this.getData()[aclEntryOffset] = aclEntry;
	}
	public byte[] getMsdu() {
		byte[] msdu = new byte[getMsduLength()];
		System.arraycopy( this.getData(), msduOffset,msdu, 0, getMsduLength());
		return msdu;
	}
	public void setMsdu(byte[] msdu) {
		if(msdu != null)
		{
			setMsduLength((byte) msdu.length);
			System.arraycopy( msdu, 0,this.getData(), msduOffset, msdu.length);
		}
		else
		{
			setMsduLength((byte) 0);
		}
	}
}
